package com.hundsun.fmbp.deploy.bean;

import org.springframework.util.StringUtils;

import lombok.Data;

/**
 * 数据库配置POJO
 * 
 * @ClassName: DBConfigBean
 * @author chenc
 * @date 2017年2月17日 下午1:27:30
 *
 */
@Data
public class DBConfigBean {

    private String ip;
    private String port;
    private String sid;
    private String driverClass;
    private String username;
    private String password;
    private String execSqlUrl;
    private boolean isServerName;
    private String alias;

    public DBConfigBean() {
        this.driverClass = "oracle.jdbc.driver.OracleDriver";
    }

    public DBConfigBean(String ip, String port, String sid) {
        this();
        this.ip = ip;
        this.port = port;
        this.sid = sid;
    }

    public DBConfigBean(String ip, String port, String sid, String username, String password, boolean isServerName) {
        this(ip, port, sid);
        this.username = username;
        this.password = password;
        this.isServerName = isServerName;
        this.buildUrl();
    }

    /**
     * 根据ip和port生成oracle的url
     * 
     * @Title: buildUrl
     */
    public void buildUrl() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.username).append("/").append(this.password);
        sb.append("@");
        if (isServerName) {
            sb.append("//");
        }
        sb.append(this.ip).append(":").append(this.port);
        sb.append("/").append(this.sid);
        this.execSqlUrl = sb.toString();
    }

    public void modifyUrl(String alias) {
        if (StringUtils.isEmpty(alias)) {
            return;
        }
        StringBuilder sb = new StringBuilder();
        sb.append(this.username).append("/").append(this.password);
        sb.append("@").append(alias);
        this.execSqlUrl = sb.toString();
    }

}
